<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
        <div id="searchBar2" style="padding:5px;height:auto">
            <div>
                角色名称: <input class="easyui-textbox" id="roleName_sch"/>
                绑定状态:
                <select class="easyui-combobox" id="bindState_sch" panelHeight="auto" style="width:80px">
                    <option value="1" selected="selected">已绑定</option>
                    <option value="0">未绑定</option>
                </select>
                <input type="hidden" th:value="${userId}" id="userId_sch"/>
                <a href="javascript:searchForm2()" class="easyui-linkbutton" iconCls="icon-search">查询</a>
                <a href="javascript:doBind()"  class="easyui-linkbutton" iconCls="icon-add" style="display:none;" id="doBtn">绑定</a>
                <a href="javascript:unBind()"  class="easyui-linkbutton" iconCls="icon-remove" id="unBtn">解绑</a>
            </div>
        </div>
        <table id="listTb2" style="width:100%;height:345px">
            <thead>
            <tr>
                <th data-options="field:'roleId',width:70,checkbox:true">角色ID</th>
                <th data-options="field:'roleName',width:200">角色名</th>
                <th data-options="field:'opt',width:50,align:'center',
                formatter: function(val,row,index){
                        if(row.userId){
                            return '是';
                        }else{
                            return '否';
                        }
                }">是否绑定</th>

                <th data-options="field:'createTime',width:140,align:'center',sortable:true,
                formatter: function(val,row,index){
                        return timeStamp2String(val);
                    }">创建时间</th>
                <th data-options="field:'createUser',width:70">创建人ID</th>
            </tr>
            </thead>
        </table>


<script th:inline="javascript">
/*<![CDATA[*/
   var canUserBindRoleOpt = /*[[${authMap.canUserBindRoleOpt}]]*/ false;

    $(function(){
        $('#listTb2').datagrid({
            url :'/user/query_bind_role',
            method:'get',
            fitColumns : true,
            striped : true,
            rownumbers : true,
            border : false,
            pagination : true,
            pageSize : 10,
            pageList : [10, 20, 30, 40, 50],
            pageNumber : 1,
            sortName : 'createTime',
            sortOrder : 'DESC',
            queryParams: {
                userId: $("#userId_sch").val(),
                bindState: 1
            },
            toolbar:$('#searchBar2')
        });

        if(!canUserBindRoleOpt){
            $("#doBtn").hide();
            $("#unBtn").hide();
        }
    })


    function searchForm2() {
        var bindState=$("#bindState_sch").combobox('getValue');
        $('#listTb2').datagrid('reload',{
            roleName: $("#roleName_sch").val(),
            bindState: bindState,
            userId: $("#userId_sch").val()
        });
        if(!canUserBindRoleOpt){
            return;
        }
        if(bindState==1){ //查询已绑定的
            $("#doBtn").hide();
            $("#unBtn").show();
        }else{ //查询未绑定的
            $("#unBtn").hide();
            $("#doBtn").show();
        }
    }

    function unBind() {
        var rows = $('#listTb2').datagrid('getChecked');
        if(rows.length>0){
            var roleIds = [];
            $.each(rows, function(index, item){
                roleIds.push(item.roleId);
            });
            bindOpt(0,roleIds.join("-"));
        }else{
            showMsg('请选择一条记录');
        }
    }
    
    function doBind() {
        var rows = $('#listTb2').datagrid('getChecked');
        if(rows.length>0){
            var roleIds = [];
            $.each(rows, function(index, item){
                roleIds.push(item.roleId);
            });
            bindOpt(1,roleIds.join("-"));

        }else{
            showMsg('请选择一条记录');
        }
    }
    function bindOpt(bindState,roleIds){
        $.messager.progress();
        $.ajax({
            type:'get',
            contentType: "application/json; charset=utf-8",
            url:"/user/bind_role?bindState="+bindState+"&userId="+$("#userId_sch").val()+"&roleIds="+roleIds,
            cache:false,
            dataType:'json',
            success:function(data){
                if(data.code!=0){
                    $.messager.alert('提示', data.msg, 'error');
                }else{
                    searchForm2();
                    showMsg('提交成功');
                }
            },
            error:function(e) {
                $.messager.alert('提示', '提交失败', 'error');

            },
            complete:function () {
                $.messager.progress('close');
            }
        });
    }
/*]]>*/
</script>
</body>
</html>